package com.webssky.jcseg.main;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import com.webssky.jcseg.core.Config;
import com.webssky.jcseg.core.ISegment;
import com.webssky.jcseg.core.IWord;
import com.webssky.jcseg.core.JCSegException;
import com.webssky.jcseg.core.SegmentFacotry;

public class Test {
	
	public static String segment(Reader reader, int type) throws JCSegException, IOException {
		
		StringBuffer sb = new StringBuffer();
		
		String __segClass = null;
		if ( type == Config.SIMPLE_MODE ) 
			__segClass = "com.webssky.jcseg.SimpleSeg";
		else if ( type == Config.COMPLEX_MODE ) 
			__segClass = "com.webssky.jcseg.ComplexSeg";
		else 
			throw new JCSegException("unknow segment algorithm.");
		
		long start = System.currentTimeMillis();
		ISegment seg = SegmentFacotry.createSegment(__segClass);
		seg.reset(reader);
		System.out.println("Diciontary Loaded, cost:"+(System.currentTimeMillis() - start)+" msec");
		//seg.setLastRule(null);
		IWord word = null;
		
		long _start = System.currentTimeMillis();
		while ( (word = seg.next()) != null ) {
			sb.append(word.getValue());
			sb.append("  ");
		}
		System.out.println("Done, cost:"+(System.currentTimeMillis() - _start)+" msec");
		System.out.println("总字数："+seg.getStreamPosition());
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filename = "/home/eric/tmp/index.html";
		if (  args.length >= 1 ) 
			filename = args[0];
		try {
			segment(new BufferedReader(
					new InputStreamReader(
							new FileInputStream(filename), "UTF-8")),
					Config.COMPLEX_MODE);
			//System.out.println("Complex-> "+segment(sb.toString(), Config.COMPLEX_MODE));
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

}
